package com.directv.z8training.ex7;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author HieuLT8
 *
 */
public class RunDemo {
	public static void main(String[] args) {
		Map<Integer, Person1> map = new HashMap<Integer, Person1>();
		Map<Integer, Person1> mapCon = new ConcurrentHashMap<Integer, Person1>();
		Person1 p1 = new Person1("An", "HCM", 20);
		Person1 p2 = new Person1("Binh", "HCM", 5);
		Person1 p3 = new Person1("Tuan", "HCM", 6);
		Person1 p4 = new Person1("Chau", "HCM", 11);
		Person1 p5 = new Person1("Thai", "HCM", 20);
		Person1 p6 = new Person1("Dung", "HCM", 21);
		Person1 p7 = new Person1("Hoang", "HCM", 22);
		Person1 p8 = new Person1("Kiet", "HCM", 23);
		Person1 p9 = new Person1("Lan", "HCM", 24);
		Person1 p10 = new Person1("Tien", "HCM", 5);
		Person1 p11 = new Person1("Bich", "HCM", 6);
		Person1 p12 = new Person1("Minh", "HCM", 10);
		Person1 p13 = new Person1("An", "HCM", 20);
		Person1 p14 = new Person1("Binh", "HCM", 5);
		Person1 p15 = new Person1("Tuan", "HCM", 6);
		Person1 p16 = new Person1("Chau", "HCM", 11);
		Person1 p17 = new Person1("Thai", "HCM", 20);
		Person1 p18 = new Person1("Dung", "HCM", 21);
		Person1 p19 = new Person1("Hoang", "HCM", 22);
		Person1 p20 = new Person1("Kiet", "HCM", 23);
		Person1 p21 = new Person1("Lan", "HCM", 24);
		Person1 p22 = new Person1("Tien", "HCM", 5);
		Person1 p23 = new Person1("Bich", "HCM", 6);
		Person1 p24 = new Person1("Minh", "HCM", 10);
		map.put(1, p1);
		map.put(2, p2);
		map.put(3, p3);
		map.put(4, p4);
		map.put(5, p5);
		map.put(6, p6);
		map.put(7, p7);
		map.put(8, p8);
		map.put(9, p9);
		map.put(10, p10);
		map.put(11, p11);
		map.put(12, p12);
		map.put(13, p13);
		map.put(14, p14);
		map.put(15, p15);
		map.put(16, p16);
		map.put(17, p17);
		map.put(18, p18);
		map.put(19, p19);
		map.put(20, p20);
		map.put(21, p21);
		map.put(22, p22);
		map.put(23, p23);
		map.put(24, p24);
		Map<Integer, Person1> syncMap = Collections.synchronizedMap(map);

		mapCon.put(1, p1);
		mapCon.put(2, p2);
		mapCon.put(3, p3);
		mapCon.put(4, p4);
		mapCon.put(5, p5);
		mapCon.put(6, p6);
		mapCon.put(7, p7);
		mapCon.put(8, p8);
		mapCon.put(9, p9);
		mapCon.put(10, p10);
		mapCon.put(11, p11);
		mapCon.put(12, p12);
		mapCon.put(13, p13);
		mapCon.put(14, p14);
		mapCon.put(15, p15);
		mapCon.put(16, p16);
		mapCon.put(17, p17);
		mapCon.put(18, p18);
		mapCon.put(19, p19);
		mapCon.put(20, p20);
		mapCon.put(21, p21);
		mapCon.put(22, p22);
		mapCon.put(23, p23);
		mapCon.put(24, p24);

		for (Map.Entry<Integer, Person1> entry : map.entrySet()) {
			System.out.println("ID: " + entry.getKey() + "----" + "Person: "
					+ entry.getValue().getName());
			
		}
		System.out.println("=============================================");
		
		
		for (Map.Entry<Integer, Person1> entry : mapCon.entrySet()) {
			System.out.println("ID: " + entry.getKey() + "----" + "Person: "
					+ entry.getValue().getName());
		}
		
		
		System.out.println("Iterator");
		long stimesync = System.currentTimeMillis();
		Iterator<Map.Entry<Integer, Person1>> iter = map.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry<Integer, Person1> entry = iter.next();
			System.out.println("ID: " + entry.getKey() + "----" + "Person: "
					+ entry.getValue().getName());
		}
		long etimesync = System.currentTimeMillis();
		System.out.println("Time for Collection Sync: " + (etimesync - stimesync));
		
		System.out.println("Iterator Concurrent");
		long stime = System.currentTimeMillis();
		Iterator<Map.Entry<Integer, Person1>> iter1 = mapCon.entrySet().iterator();
		while (iter1.hasNext()) {
			Map.Entry<Integer, Person1> entry = iter1.next();
			System.out.println("ID: " + entry.getKey() + "----" + "Person: "
					+ entry.getValue().getName());
		}
		long etime = System.currentTimeMillis();
		System.out.println("Time for Concurrent: " + (etime - stime));
	}
}
